<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Statistics extends Controller
{
    /**
     * 首页统计
     *
     * @return \think\Response
     */
    public function statistics()
    {
//        核查通知书
        $statistics['inspect_notice_nums']=Db::name('Notice')->where('type','eq',1)->count();
//        告知书
        $statistics['notification_nums']=Db::name('Notice')->where('type','eq',2)->count();
//        核查数
        $statistics['inspect_nums']=Db::name('Notice')->count();
//        不合规处理数
        $statistics['no_compliance_nums']=Db::name('Notice')->where('type','eq',2)->count();
//        民主生活会说明
        $statistics['democracy_life_nums']=Db::name('Notice')->where('type','eq',2)->count();
//        公示数
        $statistics['publicity_nums']=Db::name('Notice')->where('type','eq',2)->count();
//        干部人数
        $statistics['cadre_nums']=Db::name('Cadre_relative')->where('relationship','本人')->count();
//        亲属人数
        $statistics['relatives_nums']=Db::name('Cadre_relative')->where('relationship','neq','本人')->count();
//        受益人次
        $statistics['benefit_frequency']=Db::name('Income')->count('id');
//        受益人数
        $statistics['benefit_nums']=Db::name('Income')->group('sfid')->count();
//        涉权人次

//        涉权人数
//        涉权金额
//        涉权项目
//        受益资金走势
        $sfid=Db::name('Cadre_relative')->where('relationship','本人')->field('sfid')->select();
        $q_sfid=Db::name('Cadre_relative')->where('relationship','neq','本人')->field('q_sfid')->select();
        foreach($sfid as $k=>$v){
            $cadre_sfid[]=$v['sfid'];
        }
        foreach($q_sfid as $j=>$l){
            $relatives_sfid[]=$l['q_sfid'];
        }
//      干部
        $cadre_funds_trend=Db::name('Income')->where('sfid','in',$cadre_sfid)->field('release_time')->group('release_time')->select();
        foreach ($cadre_funds_trend as  $k=>$v) {
            $statistics['cadre_funds_trend'][$k]['month']=date('n',$v['release_time']);
            $statistics['cadre_funds_trend'][$k]['price']=Db::name('Income')->where('sfid','in',$cadre_sfid)->where('release_time','eq',$v['release_time'])->group('release_time')->sum('price');
        }
//        亲属
        $relatives_funds_trend_time=Db::name('Income')->where('sfid','in',$relatives_sfid)->field('release_time')->group('release_time')->select();
        foreach ($relatives_funds_trend_time as  $k=>$v) {
            $statistics['relatives_funds_trend'][$k]['month']=date('n',$v['release_time']);
            $statistics['relatives_funds_trend'][$k]['price']=Db::name('Income')->where('sfid','in',$relatives_sfid)->where('release_time','eq',$v['release_time'])->group('release_time')->sum('price');
        }
//        处分类型统计
        $statistics['organize_punish']=Db::name('Punish')->where('punish','eq','1')->count();
        $statistics['government_punish']=Db::name('Punish')->where('punish','eq','2')->count();
        $statistics['party_discipline_punish']=Db::name('Punish')->where('punish','eq','3')->count();
        $statistics['other_punish']=Db::name('Punish')->where('punish','eq','4')->count();
//        干部亲属人数
        $county_unit=Db::name('Department')->where('name','eq','县级部门')->field('id,name')->find();
        $county_unit_sub=Db::name('Department')->where('pid','eq',$county_unit['id'])->field('id,name')->select();
        foreach($county_unit_sub as $k=>$v){
            $county_unit_sub_name[]=$v['name'];
        }
        $statistics['cadre_nums_county']=Db::name('Cadre_relative')->where('relationship','eq','本人')->where('company_id','in',$county_unit_sub_name)->count();
        $statistics['relatives_nums_county']=Db::name('Cadre_relative')->where('relationship','neq','本人')->where('company_id','in',$county_unit_sub_name)->count();
        $township_unit=Db::name('Department')->where('name','eq','乡镇（街道）')->field('id,name')->find();
        $township_unit_sub=Db::name('Department')->where('pid','eq',$township_unit['id'])->field('id,name')->select();
        foreach($township_unit_sub as $k=>$v){
            $township_unit_sub_name[]=$v['name'];
        }
        $statistics['cadre_nums_township']=Db::name('Cadre_relative')->where('relationship','eq','本人')->where('company_id','in',$township_unit_sub_name)->count();
        $statistics['relatives_nums_township']=Db::name('Cadre_relative')->where('relationship','neq','本人')->where('company_id','in',$township_unit_sub_name)->count();
        $enterprise_unit=Db::name('Department')->where('name','eq','企业')->field('id,name')->find();
        $enterprise_unit_sub=Db::name('Department')->where('pid','eq',$enterprise_unit['id'])->field('id,name')->select();
        foreach($enterprise_unit_sub as $k=>$v){
            $enterprise_unit_sub_name[]=$v['name'];
        }
        $statistics['cadre_nums_enterprise']=Db::name('Cadre_relative')->where('relationship','eq','本人')->where('company_id','in',$enterprise_unit_sub_name)->count();
        $statistics['relatives_nums_enterprise']=Db::name('Cadre_relative')->where('relationship','neq','本人')->where('company_id','in',$enterprise_unit_sub_name)->count();
//        单位涉权金额占比
//        $statistics['unit_involved_amount_proportion']=Db::name('');
        $statistics['time']=strtotime('2018/05/09');
        $statistics['time_s']=time();
        if ($statistics) {
            $datas['status']=1;
            $datas['info']=$statistics;
        }else{
            $datas['status']=1;
            $datas['info']='无数据';
        }
        return json($datas);
    }
}
